
import android.content.Context
import android.graphics.Bitmap
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import zp_print_bigdataSDK.zpPrinter

/**
 * You might think you can read it or optimize it, but no,you're wrong, Please  believe me. You'll see to do.
 * author:wzg
 * createTime: 2021/12/13/013 16:22
 * desc: todo...
 */
class BlePrintManager() {


    companion object {


        /**
         * @param mContext  context
         * @param bleAddress ble address
         * @param isLoop   false
         * @param gotopaper  1
         * @param listener  callback
         */
        fun printZicoxCS3(
            mContext: Context,
            bleAddress: String,
            bitmap: MutableList<Bitmap>,
            gotopaper: Int
        ) {
            when (gotopaper) {
                1 -> {//标准标签纸
                    printAction(mContext, bleAddress, bitmap, 1)
                }
                0 -> {//

                }
            }

        }


        private var index = 0

        /**
         * 处理了打印的具体逻辑
         */
        private fun printAction(
            mContext: Context,
            bleAddress: String,
            bitmap: MutableList<Bitmap>,
            gotopaper: Int
        ) {
            zp_print_bigdataSDK_Printer(mContext, bleAddress, true, gotopaper,
                object : PrintListener {

                    override fun getBitMap(): Bitmap {
                        return bitmap[index]
                    }

                    override fun error(errorMsg: String?) {
                        Toast.makeText(
                            mContext, errorMsg,
                            Toast.LENGTH_LONG
                        ).show()
                    }

                    override fun end() {
                        index++
                        if (index == bitmap.size) {
                            Toast.makeText(
                                mContext, "打印完成！！！",
                                Toast.LENGTH_LONG
                            ).show()
                        } else {//递归打印
                            Handler(Looper.getMainLooper()).postDelayed({
                                Toast.makeText(
                                    mContext, "正在打印下一个",
                                    Toast.LENGTH_LONG
                                ).show()
                                printAction(mContext, bleAddress, bitmap, gotopaper)
                            }, 1000)

                        }
                    }

                })
        }


        /**
         *
         */
        private fun zp_print_bigdataSDK_Printer(
            mContext: Context,
            BDAddress: String,
            loop: Boolean,
            gotopaper: Int,
            listener: PrintListener
        ) {

            //
            val zpPrinterCur = zpPrinter(mContext)
            listener.start()
            if (!zpPrinterCur.connect(BDAddress)) {
                listener.error("连接失败------ $BDAddress")
                return
            }
            Handler(Looper.getMainLooper()).postDelayed({
                val bmp: Bitmap = listener.getBitMap()
                if (bmp != null) {
                    //
                    zpPrinterCur.Draw_Page_Bitmap_(bmp, gotopaper) //0 打完停止  1定位到间隙  2-3左右黑标
                    try {
                        Thread.sleep(500) //图片数据过大时，加延迟
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }
                    zpPrinterCur.printerStatus()
                    //处理结果
                    cs3_print_result(zpPrinterCur.GetStatus(), listener)
                    if (!loop) {
                        zpPrinterCur.disconnect()
                    }
                } else {
                    listener.error("获取打印内容失败,请重试")
                }
            }, 1200)
        }


        private fun cs3_print_result(status: Int, listener: PrintListener) {
            when (status) {
                -1 -> { //"获取状态异常------";
                    listener.error("获取状态异常-----")
                }
                1 -> { //"缺纸----------";
                    listener.error("缺纸----------")
                }
                2 -> { //"开盖----------";
                    listener.error("开盖----------")
                }
                0 -> { //"打印机正常-------";
                    listener.end()
                }
            }
            LoggerR.e("cs3_print_result ==  status: $status")
        }

    }


}